package ask.speedy;

import java.util.Vector;

import speedy.ask.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import ask.speedy.basic.Modem;
import ask.speedy.basic.ModemContent;
import ask.speedy.basic.TutorialVideo;
import ask.speedy.util.ContainerParser;

/**
 * @author passa
 *
 */
public class TutorialActivity extends Activity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // (NEW)
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN); // (NEW)
                
        setContentView(R.layout.linearlayout);
        
        getView(this);
    }
    
    private void getView(final Context context) {
    	
    	Vector<Modem> modems = ContainerParser.modems;
    	Vector<TutorialVideo> videos = ContainerParser.videos;
    	
    	final LinearLayout layout_modemlist = (LinearLayout)this.findViewById(R.id.tutorial_modemlist);
    	layout_modemlist.setVisibility(LinearLayout.GONE);
    	
    	TextView textlayout_modemlist = (TextView)this.findViewById(R.id.tutorial_modemlist_videotext);
    	
    	textlayout_modemlist.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (layout_modemlist.getVisibility() == LinearLayout.GONE) {
					layout_modemlist.setVisibility(LinearLayout.VISIBLE);
					
				} else {
					layout_modemlist.setVisibility(LinearLayout.GONE);
				}
			}
		});
    	
    	for (int i = 0; i < modems.size(); ++i) {
    		
    		Modem aModem = modems.get(i);
    		
    		final LinearLayout layout_aModem = new LinearLayout(this);
    		layout_aModem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    		layout_aModem.setVisibility(LinearLayout.GONE);
    		layout_aModem.setOrientation(LinearLayout.VERTICAL);
    		
    		TextView aModemName = new TextView(this);
    		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    		lp.setMargins(0, 3, 0, 0);
    		aModemName.setLayoutParams(lp);
    		aModemName.setBackgroundColor(Color.rgb(255, 156, 169));
    		aModemName.setText(aModem.getName());
    		aModemName.setGravity(Gravity.CENTER_VERTICAL);
    		aModemName.setPadding(20, 0, 0, 0);
    		aModemName.setHeight(70);
    		aModemName.setTextColor(Color.BLACK);
    		
    		aModemName.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// modem name listener
					if (layout_aModem.getVisibility() == LinearLayout.GONE) {
						layout_aModem.setVisibility(LinearLayout.VISIBLE);
					} else {
						layout_aModem.setVisibility(LinearLayout.GONE);
					}
				}
    		});
    		
    		Vector<ModemContent> modemContents = aModem.getModem_content();
    		
    		for (int j = 0; j < modemContents.size(); ++j) {
    			ModemContent aModemContent = modemContents.get(j);
    			
    			TextView aModemItemName = new TextView(this);
        		aModemItemName.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        		aModemItemName.setText(aModemContent.getTitle());
        		aModemItemName.setHeight(70);
        		aModemItemName.setGravity(Gravity.CENTER_VERTICAL);
        		aModemItemName.setPadding(25, 0, 0, 0);
        		aModemItemName.setBackgroundColor(Color.rgb(250, 187, 196));
        		aModemItemName.setTextColor(Color.BLACK);
        		
        		final LinearLayout modemItemContent = new LinearLayout(this);
        		modemItemContent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        		modemItemContent.setVisibility(LinearLayout.GONE);
        		modemItemContent.setOrientation(LinearLayout.VERTICAL);
        		
        		Vector<String> modemitemcontent = aModemContent.getContent();
        		for (int k = 0; k < modemitemcontent.size(); ++k) {
        			String aContent = modemitemcontent.get(k);
        			
        			TextView aModemItemContent = new TextView(this);
        			aModemItemContent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        			aModemItemContent.setText(aContent);
        			aModemItemContent.setHeight(50);
        			aModemItemContent.setGravity(Gravity.CENTER_VERTICAL);
        			aModemItemContent.setPadding(30, 0, 0, 0);
        			aModemItemContent.setBackgroundColor(Color.rgb(250, 222, 226));
        			aModemItemContent.setTextColor(Color.BLACK);
        			
        			modemItemContent.addView(aModemItemContent);
        		}
        		
        		aModemItemName.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (modemItemContent.getVisibility() == LinearLayout.GONE) {
							modemItemContent.setVisibility(LinearLayout.VISIBLE);
						} else {
							modemItemContent.setVisibility(LinearLayout.GONE);
						}
					}
				});
        		
        		layout_aModem.addView(aModemItemName);
        		layout_aModem.addView(modemItemContent);
    		}
    		
    		layout_modemlist.addView(aModemName);
    		layout_modemlist.addView(layout_aModem);
    		
    	}
    	
    	
    	/**VIDEO THINGS */
    	
    	final LinearLayout layout_videolist = (LinearLayout)this.findViewById(R.id.tutorial_videolist);
    	layout_videolist.setVisibility(LinearLayout.GONE);
    	
    	TextView textlayout_videolist = (TextView)this.findViewById(R.id.tutorial_videolist_videotext);
    	
    	textlayout_videolist.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (layout_videolist.getVisibility() == LinearLayout.GONE) {
					layout_videolist.setVisibility(LinearLayout.VISIBLE);
				} else {
					layout_videolist.setVisibility(LinearLayout.GONE);
				}
			}
		});
    	
    	for (int i = 0; i < videos.size(); ++i) {
    		
    		final TutorialVideo aVideo = videos.get(i);
    		TextView textlayout_video = new TextView(this);
    		
    		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    		lp.setMargins(0, 3, 0, 0);
    		textlayout_video.setLayoutParams(lp);
    		textlayout_video.setBackgroundColor(Color.rgb(255, 156, 169));
    		textlayout_video.setText(aVideo.getName());
    		textlayout_video.setGravity(Gravity.CENTER_VERTICAL);
    		textlayout_video.setPadding(20, 0, 0, 0);
    		textlayout_video.setHeight(70);
    		textlayout_video.setTextColor(Color.BLACK);
    		
    		textlayout_video.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//pop up url video
					
					Intent myIntent = new Intent(Intent.ACTION_VIEW);
					myIntent.setData(Uri.parse(aVideo.getUrl()));
					startActivity(myIntent);
				}
			});
    		
    		layout_videolist.addView(textlayout_video);
    	}
    	
    }
    
    
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
}
